﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ultima;
using SpellCaster3.Forms.Controls;
using Expand;

namespace SpellCaster3.Forms
{
    public partial class HuePicker : Form,  IInputOutput
    {
        public static readonly int MaxHues = Hues.List.Length;

        public HuePicker()
        {
            InitializeComponent();
            _HueControls = new List<HueControl>(vScrollBar.LargeChange);

            vScrollBar.Minimum = 1;
            vScrollBar.Maximum = MaxHues;
            hueSearch.Minimum = 1;
            hueSearch.Maximum = MaxHues;

            vScrollBar.Value = 1;
            RenderHues();

            DialogResult = DialogResult.Cancel;
        }

        public HuePicker(int hueIndex) : this() { Value = hueIndex; }

        private List<HueControl> _HueControls;

        private void RenderHues()
        {
            HueControl current;

            for (int i = vScrollBar.Value; i < (vScrollBar.LargeChange + vScrollBar.Value); ++i)
            {
                if (_HueControls.Count < vScrollBar.LargeChange)
                {
                    current = new HueControl(i);
                    _HueControls.Add(current);
                    huesPanel.Controls.Add(current);
                    current.Location = new Point(0, (i - vScrollBar.Value) * 20);
                    current.Click += new EventHandler(Hue_Click);
                }
                else
                {
                    current = _HueControls[i - vScrollBar.Value];
                    current.HueIndex = i;
                }
            }
        }

        private void Hue_Click(object sender, EventArgs e)
        {
            HueControl current = sender as HueControl;
            DialogResult = DialogResult.OK;
            Value = current.HueIndex;
            Close();
        }

        private void vScrollBar_ValueChanged(object sender, EventArgs e)
        {
            RenderHues();
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            vScrollBar.Value = (int)hueSearch.Value;
        }

        private void hueSearch_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                e.Handled = true;
                searchButton.PerformClick();
            }
        }

        private int? _LastValue;

        private int _Value;
        public int Value
        {
            get { return _Value; }
            set
            {
                _LastValue = _Value;
                _Value = value;
                OnValueChanged(new LastValueEventArgs<int?>(_LastValue));
                OnIOValueChanged(new LastValueEventArgs<object>(_LastValue));
                vScrollBar.Value = _Value;
            }
        }

        protected void OnValueChanged(LastValueEventArgs<int?> e)
        {
            if (ValueChanged != null) ValueChanged(this, e);
        }

        protected void OnIOValueChanged(LastValueEventArgs<object> lastValue)
        {
            if (IOValueChanged != null) IOValueChanged(this, lastValue);
        }

        public event EventHandler<LastValueEventArgs<int?>> ValueChanged;

        public object IOValue
        {
            get { return Value; }
            set
            {
                Value = (int)value;
            }
        }

        public event EventHandler<LastValueEventArgs<object>> IOValueChanged;

        private void cancelButton_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
